
local w,h = 130,10
--重复利用组
local group1 = {}
local remove1 = {}

local buffs = {
    '双击复活',
    '技能蓄力',
    '击飞',
    '晕眩',
    '蓄力中',
    '被锁定',
}
local function create_life(w,h)
    local panel = class.panel:builder
    {
        x = -800,
        y = -200,
        w = w,
        h = h,
        scale = 1,
        is_show = false,
        world_auto_remove = false,
        parent = ac.frame.portrait,
        world_anchor = 'bottom',
        normal_image = [[ui\血条\血条底.tga]],
        buffer_rate = 1,
        updata_buff = function(self,unit)
            for _,name in ipairs(buffs) do
                local buff = unit:find_buff(name)
                if buff then
                    local title = buff:get_title()
                    if self.title.text~=title then
                        self.title:set_text(title)
                    end
                    if buff.time>0 then
                        self.state:set_width(self.max_w*buff:get_remaining()/buff.time)
                    else
                        self.state:set_width(self.max_w)
                    end
                    self.state:show()
                    return
                end
            end
            local title = unit.name
            if unit.owner:is_player() then
                title = unit.owner:get_world_name()
            end
            if self.title.text~=title then
                self.title:set_text(title)
            end
            self.state:hide()
        end,
        update = function(self)
            local player = ac.player.self
            local unit = self.world_widget
            local life = unit:get '生命'
            local hpmax = unit:get '生命上限'
            local rate = life/hpmax
            
            if rate>self.buffer_rate then
                self.buffer_rate = math.min(self.buffer_rate + 0.01,rate)
            elseif rate<self.buffer_rate then
                self.buffer_rate = math.max(self.buffer_rate - 0.01,rate)
            end
            if unit.hide_buffer==nil and self.buffer_rate~=rate then
                local start = math.min(rate,self.buffer_rate)
                local size = math.abs(rate - self.buffer_rate)
                self.buffer:set_position(self.max_w* start * self.scale,self.buffer.y)
                self.buffer:set_width(self.max_w* size * self.scale)
                self.buffer:show()
            else
                self.buffer:hide()
            end

            self.life:set_width(self.max_w* rate * self.scale)
            if self.updata_shield then
                self:updata_shield(unit)
            end
            -- if self.updata_buff then
            --     self:updata_buff(unit)
            -- end
            local title = unit.ui_title or unit:get_name()
            if self.title.text~=title then
                self.title:set_text(title)
            end
        end,
    }

    --panel.state = panel:add_panel([[core\hpbar\黄条.tga]])
    panel.life = panel:add_panel([[ui\血条\血条.tga]])
    panel.fill = panel:add_panel([[ui\血条\BOSS血条框.tga]],-5,-6,panel.w + 10,panel.h + 12)

    panel.buffer = panel.life:add_panel([[core\hpbar\ShieldBar.tga]])
    panel.buffer:set_alpha(0.5)
    panel.max_w = panel.life.w
    panel.life_h = panel.life.h
    panel.scale = 0.6
    panel:set_relative_size(panel.scale)
    
    panel.title = class.text:builder {
        parent = panel,
        x = -50,
        y = -62,
        w = panel.w+100,
        h = 60,
        color = 0xffffffff,
        shadow_offset_x = 4,
        shadow_offset_y = -4,
        shadow_color = 0xff000000,
        outline = true,
        show_flag = true,
		font_size = 12,
        align = 2,
    }
    panel:set_level(40)
    panel:hide()

    return panel
end

local remove2 = {}
local group2 = {}
local w,h = 351,23
function unit_add_life4(unit)
    if group2[unit.handle] then
        return group2[unit.handle]
    end
    local panel = table.remove(remove2,1)
    if not panel then
        panel = create_life(w,h)
    end

    panel.buffer_rate = 1

    local title = unit.ui_title or unit:get_name()
    panel.title:set_text(title)
    
    panel:bind_world(unit)
    japi.SetUnitPressUIVisible(unit.handle,false)
    group2[unit.handle] = panel

    unit:event '单位-即将移除'(function()
        group2[unit.handle] = nil
        table.insert(remove2,panel)
    end)

    return panel
end

ac.hardware:on "界面绘制" (function ()
    for handle,panel in pairs(group2) do
        if panel.show_flag then
            panel:update()
        end
    end
end)